package com.admin.mds.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.admin.mds.system.mapper.SysMenuMapper;
import com.admin.mds.system.mapper.SysRoleMapper;
import com.admin.mds.system.mapper.SysUserMapper;
import com.admin.mds.system.model.entity.SysMenuEntity;
import com.admin.mds.system.model.entity.SysRoleEntity;
import com.admin.mds.system.model.entity.SysUserEntity;
import com.admin.mds.system.service.LoginService;
import com.admin.security.exception.UserLoginBusinessException;
import com.admin.utils.constant.GlobleSysUser;







@Service
@Primary
public class LoginServiceImpl implements  LoginService{
	
	@Autowired
	private SysUserMapper sysUserMapper;
	
	@Autowired
	private SysMenuMapper sysMenuMapper;
		
	@Autowired
	private SysRoleMapper sysRoleMapper;
	

	/**
	 * 登录查询
	 */
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		SysUserEntity  user = new SysUserEntity();
		user.setUserName(username);		
		List<SysUserEntity> userVoList = sysUserMapper.getExctList(user);
		if(userVoList == null || userVoList.isEmpty()) {
			throw new UserLoginBusinessException("用户不存在");
		}			
		SysUserEntity userVo = userVoList.get(0);
		return userToGlobleUser(userVo);
	}
		
	/**
	 * 复制用户其他信息
	 * @param userVo
	 * @return
	 */
	private GlobleSysUser userToGlobleUser(SysUserEntity userVo ) {
		List<String> perms = addAuthority(userVo.getUserId());		
		List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();	
		for(String perm : perms) {
			authorities.add(new SimpleGrantedAuthority(perm));			
		}
		//权限
		GlobleSysUser  gs = new GlobleSysUser(userVo.getUserName(), userVo.getPassword(),authorities ,new Date().getTime());
		userVo.setPermissions(perms);
		//角色
		List<SysRoleEntity> roleVoList = getUserRoles(userVo.getUserId());
		List<String>  roles = new ArrayList<>();
		Integer minRoleSort = 0;
		for(SysRoleEntity  role : roleVoList ) {
			roles.add(role.getRoleName());
			if(role.getRoleSort() >= minRoleSort) {
				minRoleSort = role.getRoleSort() ;
			}
		}
		userVo.setRoles(roles);
		userVo.setMaxRoleLevel(minRoleSort);
		BeanUtils.copyProperties(userVo, gs);
		gs.setRoleList(roleVoList);
		return gs;
	}
	
	private List<SysRoleEntity> getUserRoles(String userId){
		return sysRoleMapper.getUserRoles(userId);
	}
	
	/**
	 * 查询权限
	 * @param userId
	 * @return
	 * @throws Exception 
	 */
	private List<String> addAuthority(String userId)  {
		List<SysMenuEntity> menuList = sysMenuMapper.getMenuByRoles(userId);
		List<String>  strs = new ArrayList<>();
		for(SysMenuEntity menuVo : menuList) {
			if(menuVo.getPerms() != null &&  !menuVo.getPerms().isEmpty()) {
				strs.add(menuVo.getPerms());
			}
		}
		return strs;
	}





	

}
